package com.ocom.payconfig.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityDishesPrice;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.pojo.payconfig.MachPriceVo;
import com.ocom.common.request.payconfig.GetChangeDishesListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.MealMenuInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDishesPrice;
import com.ocom.payconfig.service.DishesPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishesPriceServiceImpl extends ServiceImpl<IDaoDishesPrice, EntityDishesPrice> implements DishesPriceService {

    @Autowired
    private IDaoDishesPrice iDaoDishesPrice;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesPrice> selectDishesPriceList(Long comId, Long merchId, List<Integer> dishesIds) {
        return iDaoDishesPrice.selectDishesPriceList(comId,merchId,dishesIds);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesPrice> getDishesPriceList(Long comId, Integer dishesId, Integer merchId) {
        QueryWrapper<EntityDishesPrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        if(merchId!=null){
            queryWrapper.eq("merch_id",merchId);
        }
        if (dishesId !=null){
            queryWrapper.eq("dishes_id",dishesId);
        }
        queryWrapper.orderByAsc("point_id","price_mode");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesPrice selectPriceDishesInfo(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesPrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        queryWrapper.eq("dishes_id",jsonObject.getInteger("dishesId"));
        queryWrapper.eq("point_id",jsonObject.getInteger("pointId"));
        queryWrapper.eq("price_mode",jsonObject.getByte("priceMode"));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean insertDishesPrice(Long comId, EntityDishesPrice dishesPrice) {
        return this.save(dishesPrice);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean updateDishesPrice(Long comId, EntityDishesPrice dishesPrice) {
        return this.updateById(dishesPrice);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean removeDishesPrice(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesPrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        queryWrapper.eq("dishes_id",jsonObject.getInteger("dishesId"));
        queryWrapper.eq("point_id",jsonObject.getInteger("pointId"));
        queryWrapper.eq("price_mode",jsonObject.getByte("priceMode"));
        return this.remove(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesPrice> getDishesAllDownload(Long comId, Integer merchId) {
        QueryWrapper<EntityDishesPrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.orderByAsc("com_id","merch_id","dishes_id");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster =  MasterSlaveEnum.SLAVE)
    public List<EntityDishesPrice> getDishesPrice(Long companyId, Integer merchId, Integer pointId) {
        QueryWrapper<EntityDishesPrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("merch_id",merchId);
        List<Integer>  ins  =new ArrayList<>();
        ins.add(0);
        if(pointId!=null){
            ins.add(pointId);
        }
        queryWrapper.in("point_id",ins);
        queryWrapper.orderByDesc("dishes_id","price_mode ","point_id");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster =  MasterSlaveEnum.SLAVE)
    public List<MachPriceVo> getMqttPrices(Long companyId, List<Integer> calssIds) {
        return iDaoDishesPrice.getMqttPrices(companyId,calssIds);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster =  MasterSlaveEnum.SLAVE)
    public Result getChangeDishesList(Long companyId, GetChangeDishesListRequest request) {
        QueryWrapper<?> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("d.com_id",request.getComId())
                .eq("d.merch_id",request.getMerchId())
                .eq("p.point_id",request.getPointId())
                .eq("p.price_mode",request.getPriceMode())
                .eq("p.price",request.getDishesPrice())
                .eq("d.status",1)
                .ne("d.dishes_id",request.getDishesId())
        ;
        return ResultUtil.success( this.iDaoDishesPrice.getMealMenuInfo(queryWrapper));
    }
}
